<?php

/**
 * 栏目管理
 */
class Category extends CI_Controller {

	var $contentCnt = 0 ;

	public function __construct() {
		parent :: __construct();
		$this->load->database();
	}

	/**
	 * 展示view/admin/category/index.php
	 */
	public function index() {
		$this->load->view('admin/category/category_view_frame');
	}

	/**
	 * 展示view/admin/category/tree.php
	 */
	public function toFrameLeft(){
		$this->load->view('admin/category/category_view_frame_left');
	}

	/**
	 * 展示view/admin/category/main.php
	 */
	public function toFrameRight(){
		$id = "";
		$name = "";
		$categoryType = "";
		if (isset($_REQUEST['id'])) {
			$id = $_REQUEST['id'];
		}else{
			$id = "0";
		}
		if($id != "0"){
			$category = $this->category_model->get($id);
			if($category != null){
				$name = $category->name ;
			}
		}

		if (isset($_REQUEST['categoryType'])) {
			$categoryType = $_REQUEST['categoryType'];
		}

		$data['id'] = $id ;
		$data['name'] = $name ;
		$data['categoryType'] = $categoryType ;

		$this->load->view('admin/category/category_view_frame_right',$data);
	}

	/**
	 * 返回栏目树请求的json数据
	 */
	public function treeJson() {
		$parent_id = "";
		if (array_key_exists('parent_id', $_REQUEST)) {
			$parent_id = $_REQUEST['parent_id'];
			//按点击的节点输出其子节点
			echo $this->category_model->getTreeJson($parent_id);
		}
		else{
			//输出根节点
			echo "[{ id:'0',name:'root',isParent:true}]";
		}
	}

	/**
	 * 转到子栏目列表页面
	 */
	public function toChildList($id , $info){

		//查询子栏目
		$data['result_array'] = $this->category_model->getByParentId($id);
		//把id带到页面
		$data['category_id'] = $id ;

		if($info == ""){
			$info = "no" ;
		}
		$data['info'] = $info ;

		$this->load->view('admin/category/category_view_list', $data);
	}

	/**
	 * 转到编辑页面
	 */
	public function toEdit($id ,$info ){

		$data['category'] = $this->category_model->get($id);

		$data['info'] = $info ;

		//echo $data['category'] ;

		$this->load->view('admin/category/category_view_edit', $data);

	}

	/**
	 * 响应修改提交，并回到修改页面
	 */
	public function update() {

		$id = $this->input->post('id');

		$category_model = $this->category_model->get( $id );

		$category_model->category_type_id = intval($this->input->post('category_type_id'));
		$category_model->name = $this->input->post('name');
		$category_model->order_no = intval($this->input->post('order_no'));

		$category_model->modifier = $this->session->userdata('userId');
		$category_model->gmt_modified = date("Y-m-d H:i:s", time());

		//修改方法
		$this->category_model->update( $id, $category_model);

		$info = "修改成功";

		$this->toEdit( $id , $info);

	}

	/**
	 * 响应列表页面的新增或者修改提交，并回到列表页面
	 */
	public function saveOrUpdate() {

		$parent_category_id = intval($this->input->post('parent_category_id'));

		$id = $this->input->post('id_update');

		//echo $id;
		$info = "";

		if($id == null) {

			//echo "saves";

			$this->category_model->category_type_id = intval($this->input->post('category_type_id_add'));
			$this->category_model->name = $this->input->post('name_add');
			$this->category_model->order_no = $this->input->post('order_add');
			$this->category_model->parent_category_id = intval($this->input->post('parent_category_id'));

			$this->category_model->creator = $this->session->userdata('userId');
			$this->category_model->gmt_create = date("Y-m-d H:i:s", time());

			//调用保存方法
			$this->category_model->save();

			$info = "添加成功";
		} else {

			//echo "update";

			$category_model = $this->category_model->get( $id );

			$category_model->category_type_id = intval($this->input->post('category_type_id_update'));
			$category_model->name = $this->input->post('name_update');
			$category_model->order_no = intval($this->input->post('order_update'));

			$category_model->modifier = $this->session->userdata('userId');
			$category_model->gmt_modified = date("Y-m-d H:i:s", time());

			//调用修改方法
			$this->category_model->update( $id, $category_model);

			$info = "修改成功";
		}

		$this->toChildList( $parent_category_id ,$info);

	}

	public function delete($id , $parent_category_id){

		$this->contentCnt = 0 ;
		$info = "" ;

		//查看栏目下是否有文章，有文章则不让删除
		$category_num = $this->db->get_where('category', array('parent_category_id' => $id))->num_rows();

		if($category_num > 0){
			$info = "栏目下存在子栏目，不能直接删除";
		}else{

			$content_num = $this->checkContentExist($id);

			if($content_num > 0){
				$info = "栏目下存在文章，不能直接删除";
			}else{

				$success = $this->category_model->delete($id);

				if($success){
					$info = "删除成功";
				}else{
					$info = "删除失败";
				}
			}
		}

		$this->toChildList( $parent_category_id , $info );

	}

	/**
	 * 递归查询栏目下的
	 */
	public function checkContentExist($categoryId){

		$this->contentCnt += $this->db->get_where('content', array('category_id' => $categoryId))->num_rows();

		//查询子栏目
		$subCategoryArray = $this->category_model->getByParentId($categoryId);

		if($subCategoryArray != null ){
			foreach($subCategoryArray as $row){
				$subCategoryId = $row['id'];

				//递归调用
				$this->checkContentExist($subCategoryId);
			}
		}

		return $this->contentCnt ;
	}
}
?>
